package org.windowkit.toolkit.widget.table.render;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.swing.table.DefaultTableCellRenderer;

/**
 * A Date Renderer like JTable.DateRenderer except you can set the format.
 * @author Eric
 */
public class DateRenderer extends DefaultTableCellRenderer {
	private static final long serialVersionUID = -1755188997058951328L;

	private DateFormat formatter;

	/**
	 * Constructor.
	 */
	public DateRenderer() {
		this(null);
	}

	/**
	 * Constructor.
	 * @param format format to render the date in
	 */
	public DateRenderer(final String format) {
		super();
		setFormat(format);
	}

	/**
	 * Set the format to use when rendering dates.
	 * @param format the format
	 */
	public void setFormat(final String format) {
		if (format != null) {
			formatter = new SimpleDateFormat(format);
		} else {
			formatter = null;
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setValue(final Object value) {
		if (formatter == null) {
			formatter = DateFormat.getDateInstance();
		}
		setText((value == null) ? "" : formatter.format(value));
	}
}